package com.lt.hr.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lt.contract.bean.utils.ExcelImport;
import com.lt.contract.bean.utils.ResponseResult;
import com.lt.hr.dao.THrManageLoginListDao;
import com.lt.hr.entity.THrEmpInfo;
import com.lt.hr.dao.THrEmpInfoDao;
import com.lt.hr.service.THrEmpInfoService;
import com.lt.report.service.AsyncService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (THrEmpInfo)表服务实现类
 *
 * @author makejava
 * @since 2020-03-11 12:12:22
 */
@Service("tHrEmpInfoService")
@Slf4j
public class THrEmpInfoServiceImpl implements THrEmpInfoService {
    @Resource
    private THrEmpInfoDao tHrEmpInfoDao;

    @Resource
    private THrManageLoginListDao tHrManageLoginListDao;

    @Autowired
    AsyncService asyncService;


    /**
     * 通过ID查询单条数据
     *
     * @return 实例对象
     */
    @Override
    public THrEmpInfo queryById( ) {
        return this.tHrEmpInfoDao.queryById();
    }

    @Override
    public List<THrEmpInfo> queryAll(Map<String, Object> map) throws Exception {
        return tHrEmpInfoDao.queryAll(map);
    }

    @Override
    public Long queryCount(Map<String, Object> map) throws Exception {
        return tHrEmpInfoDao.queryCount(map);
    }

    @Override
    public String getBankListByExcel(InputStream in, String fileName, String staffId) throws Exception {
        //选择文件中的列
        String tableName = "";
        if(fileName.contains("员工信息导入")){
            tableName ="staff";
        }else {
            log.error("上传文件不符合规定");
            return "请上次相关文件";
        }
        //创建Excel工作薄
        Workbook work = ExcelImport.getWorkbook(in, fileName);
        Sheet sheet = work.getSheetAt(0);
//        List<List<String>> list = ExcelImport.queryRepetition(sheet,8);//第8列是计费号码
        List<Map<String,Object>> list1 = new ArrayList<>();
//        for (int i = 0; i <list.size() ; i++) {
//            list1.addAll(THrEmpInfoDao.queryRepetition(list.get(i)));
//        }
//        if(list1.size()>0){
//            JSONObject jsonObject = new JSONObject();
//            jsonObject.put("如下号码在本账期已上传，请勿重复上传。", JSON.toJSONString(list1));
//            return jsonObject.toJSONString();
//        }
            List<List<THrEmpInfo>> lists =listNmEmpInfo(sheet,staffId);
            int size = lists.size();
            for (int i = 0; i <size ; i++) {
                List<THrEmpInfo> listI = lists.get(i);
                asyncService.executeTHrEmpInfo(listI);
        }
        return "上传成功";
    }

    /**
     * 导入第二发展人
     *
     * @param sheets 第二发展人
     * @return
     */
    public List<List<THrEmpInfo>> listNmEmpInfo(Sheet sheets,String staffId) throws Exception {
        List<List<THrEmpInfo>> lists =new ArrayList<>();
        List<THrEmpInfo> list = new ArrayList<>();
        Cell cell = null;
        for (int j = sheets.getFirstRowNum() + 1; j <= sheets.getLastRowNum()+1; j++) {
            Row row = sheets.getRow(j);
            int flag =200;
            if(list.size() !=0 && list.size()%flag ==0 || j == sheets.getLastRowNum()+1){
                List<THrEmpInfo> copy = new ArrayList<>(list);
                lists.add(copy);
                list.clear();
            }
            if (row == null || row.getFirstCellNum() == j || row.getCell(1) == null) {
                continue;
            }else if (ExcelImport.isAllRowEmpty(row,row)){
                continue;
            }
            THrEmpInfo tHrEmpInfo = new THrEmpInfo();
            for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {
                cell = row.getCell(y);
                if(cell==null || cell.toString().equals("")){
                    continue;
                }
                switch (y+1){
                    case 2:tHrEmpInfo.setAreaNo(ExcelImport.getCellValue(cell)); break;
                    case 3:tHrEmpInfo.setAreaName(ExcelImport.getCellValue(cell)); break;
                    case 4:tHrEmpInfo.setEmpName(ExcelImport.getCellValue(cell)); break;
                    case 5:tHrEmpInfo.setEmpPsptId(ExcelImport.getCellValue(cell)); break;
                    case 6:tHrEmpInfo.setReserved1(ExcelImport.getCellValue(cell)); break;
                    case 7:tHrEmpInfo.setDutyRailway(ExcelImport.getCellValue(cell)); break;
                    case 8:tHrEmpInfo.setEmpBelong(ExcelImport.getCellValue(cell)); break;
                    case 9:tHrEmpInfo.setEmpType(ExcelImport.getCellValue(cell)); break;
                }

            }
            tHrEmpInfo.setReserved2(staffId);
            tHrEmpInfo.setReserved3(staffId);
            list.add(tHrEmpInfo);
        }
        return lists;
    }


    /**
     * 新增数据
     *
     * @param tHrEmpInfo 实例对象
     * @return 实例对象
     */
    @Override
    public THrEmpInfo insert(THrEmpInfo tHrEmpInfo) {
        this.tHrEmpInfoDao.insert(tHrEmpInfo);
        return tHrEmpInfo;
    }

    /**
     * 修改数据
     *
     * @param tHrEmpInfo 实例对象
     * @return 实例对象
     */
    @Override
    public ResponseResult update(THrEmpInfo tHrEmpInfo) {
        Map<String,Object> map = new HashMap<>();
        map.put("loginId",tHrEmpInfo.getLoginId());
        if(tHrManageLoginListDao.queryByTag(map).getLoginTag().equals("1")){
            return new ResponseResult(false,"只有管理员能修改该数据");
        }
        int count = tHrEmpInfoDao.update(tHrEmpInfo);
        if(count >0){
            return new ResponseResult(true,"修改成功！");
        }
        return  new ResponseResult(false,"只有管理员能修改该数据");
    }

    /**
     * 通过主键删除数据
     *
     * @return 是否成功
     */
    @Override
    public boolean deleteById( ) {
        return this.tHrEmpInfoDao.deleteById() > 0;
    }
}
